package com.hc.security;

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //直接写死数据信息，可以在这里获取数据库的信息并进行验证
        //UserDetails user = User.withUsername(username)
        //        .password(passwordEncoder.encode("1234"))
        //        .authorities("Role_vip,user:list,user:update")
        //        .build();

        User user = new User(username, passwordEncoder.encode("1234"),
                AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_vip,user:list,user:update"));
        return user;
    }
}
